# 导入相应的模块
import pygame
from pygame.locals import *
import time,random

class HeroPlane:
	'''玩家飞机（英雄）'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self.bullet_list = [] # 玩家的弹夹

	def display(self):
		'''绘制玩家飞机'''
		# 绘制子弹
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)
		self.screen.blit(self.image,(self.x,self.y))
	
	def move_left(self):
		'''左移飞机'''
		self.x -= 6
		if self.x <= 0:
			self.x = 0

	def move_right(self):
		'''右移飞机'''
		self.x += 6
		if self.x >= 406:
			self.x = 406

	def move_top(self):
		'''上移飞机'''
		self.y -= 4
		if self.y <= 0:
			self.y = 0

	def move_down(self):
		'''下移飞机'''
		self.y += 4
		if self.y >= 492:
			self.y = 492

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))

class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x + 53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		'''子弹移动'''
		self.y -= 10
		if self.y <= -17:
			return True

class EnemyPlane:
	'''敌机类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(0,408))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images/e0"+".png")
		self.bullet_list = [] # 敌机的弹夹
		
	def display(self):
		'''绘制敌机'''
		#if len(self.bullet_list) > 0:
		#	print(len(self.bullet_list))
		# 绘制敌机弹道
		for eb in self.bullet_list:
			eb.display()
			if eb.move():
				self.bullet_list.remove(eb)
		self.screen.blit(self.image,(self.x,self.y))

	def move(self,hero):
		'''
		敌机消失 return 1
		英雄消失 return 2
		'''
		#result = 0
		self.y += 2
		# 敌机出屏幕
		if self.y > 568:
			return 1
		# 遍历所有玩家子弹，并执行碰撞检测，消灭敌机
		for bo in hero.bullet_list:
			if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
				hero.bullet_list.remove(bo)
				return 1
		# 遍历所有敌机子弹，并执行碰撞检测，消灭英雄
		for ebo in self.bullet_list:
			if ebo.x>hero.x+10 and ebo.x<hero.x+96 and ebo.y>hero.y+10 and ebo.y<hero.y+66:
				self.bullet_list.remove(ebo)
				return 2
		
	def fire(self):
		self.bullet_list.append(EBullet(self.screen,self.x,self.y))

	def bomb(self):
		for i in range(0,4):
			self.screen.blit(self.bomb_images[i],(self.x+50,self.y+30))
			time.sleep(0.01)
	
class EBullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x + 58
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/epd.png")

	def display(self):
		'''绘制子弹'''
		#print('show(',self.x,',',self.y,')')
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		'''子弹移动'''
		self.y += 5
		if self.y >= 568:
			return True

class Bomb:
	'''爆炸类'''
	def __init__(self,screen_temp,x,y,ftimer):
		self.screen = screen_temp
		self.x = x
		self.y = y
		self.ftimer = ftimer
		self.images = [] # 敌机爆炸效果
		self.btime = 0
		for i in range(1,5):
			self.images.append(pygame.image.load("./images/bomb"+str(i)+".png"))
		self.image = pygame.image.load("./images/bomb3.png")

	def display(self,nowTimer):
		if nowTimer-self.ftimer<=15:  # 每隔0.15秒爆炸一次
			self.screen.blit(self.images[0],(self.x+50,self.y+30))
		elif nowTimer-self.ftimer<=30:
			self.screen.blit(self.images[1],(self.x+50,self.y+30))
		elif nowTimer-self.ftimer<=45:
			self.screen.blit(self.images[2],(self.x+50,self.y+30))
		elif nowTimer-self.ftimer<= 60:
			self.screen.blit(self.images[3],(self.x+50,self.y+30))
		else:
			return True
		return False

		self.btime += 1
		if self.btime >= 4:
			return True

	def tdisplay(self):
		self.screen.blit(self.image,(self.x+50,self.y+30))

def key_control(hero_temp):
	'''键盘控制函数'''
	# 执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()
	# 获取按键信息
	pressed_keys = pygame.key.get_pressed()
	#print(pressed_keys)
	# 做判断，并执行对象的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("Left...")
		hero_temp.move_left()
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("Right...")
		hero_temp.move_right()
	if pressed_keys[K_UP] or pressed_keys[K_w]:
		print("Up...")
		hero_temp.move_top()
	elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
		print("Down...")
		hero_temp.move_down()
	
	if pressed_keys[K_SPACE]:
		print("Space...")
		hero_temp.fire()

def main():
	'''主程序函数'''
	# 创建游戏窗口
	screen = pygame.display.set_mode((512,568),0,0)

	# 创建一个游戏背景(512*1536)
	background = pygame.image.load("./images/bg2.jpg")

	# 创建玩家飞机
	hero = HeroPlane(screen)

	enemylist = [] # 存放敌机的列表
	bomblist = [] # 存放爆炸物的列表
	m = -968 # 设置画布初始位置
	timers = 0 # 设置时间戳 放大100倍
	blood = 1 # 设置英雄血量

	while True:
		# 绘制图片
		screen.blit(background,(0,m))
		m+=2 # 设置每次画布滚动距离
		if m>=-200: # 画布回滚
			m = -968

		# 绘制玩家飞机
		hero.display()

		# 执行键盘控制
		key_control(hero)

		# 随机绘制敌机
		if random.choice(range(50)) == 10:	# 设置敌机出现的概率 20%
			enemylist.append(EnemyPlane(screen))

		#print(timers)
		# 遍历敌机并绘制移动
		for em in enemylist:
			em.display() # 展示敌机位置
			em_key = em.move(hero) # 移动敌机位置
			if em_key == 1: # 敌机被消灭或开出地图自动消失
				bomb = Bomb(screen,em.x,em.y,timers)
				bomblist.append(bomb)
				enemylist.remove(em) # 从敌机列表中移除敌机
			if em_key == 2: # 敌机的炮弹击落了玩家
				blood -= 1
				if blood == 0:
					print("you die!")
					return
			if timers % 30  == 0: # 0.3秒敌机发射一枚导弹
				em.fire() # 在敌机弹夹中加入一枚导弹
				#print(timers)
		
		# 遍历所有爆炸处
		for tbomb in bomblist:
			if tbomb.display(timers):
				bomblist.remove(tbomb) # 删除已经爆炸完的对象
		'''
		for tbomb in bomblist:
			tbomb.tdisplay()
		'''

		# 更新显示
		pygame.display.update()

		# 定时显示
		time.sleep(0.01)
		timers+=1



# 判断当前是否是主运行程序，并调用
if __name__ == '__main__':
	main()